package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.basic.common.exception.RRException;
import com.basic.common.utils.*;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.sys.service.impl.SysUserServiceImpl;
import com.basic.modules.ykf.entity.*;
import com.basic.modules.ykf.model.*;
import com.basic.modules.ykf.service.*;
import com.basic.modules.ykf.utils.ExcelCellWidthStyleStrategy;
import com.basic.modules.ykf.utils.ExcelStyleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.modules.ykf.dao.YkfCustomerSubAccountDao;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;


@Service("ykfCustomerSubAccountService")
public class YkfCustomerSubAccountServiceImpl extends ServiceImpl<YkfCustomerSubAccountDao, YkfCustomerSubAccountEntity> implements YkfCustomerSubAccountService {

    @Autowired
    private YkfCustomerShopService customerShopService;

    @Autowired
    private YkfCustomerService customerService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private YkfQualityTestingService ykfQualityTestingService;

    @Autowired
    private YkfShopDistributeRuleService ruleService;
    @Autowired
    private SysUserServiceImpl sysUserService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfCustomerSubAccountEntity> params) {
        if (ObjectUtil.isNull(params.getCondition().getSearchStatus()) || params.getCondition().getSearchStatus() != 2) {
            if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
                ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
                sortParams.setSidx("startDate");
                sortParams.setOrder("asc");
                params.setSort(sortParams);
            }
        } else {
            if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
                ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
                sortParams.setSidx("startDate");
                sortParams.setOrder("desc");
                params.setSort(sortParams);
            }
        }
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper2 = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                String keyword = params.getCondition().getKeyword();
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
                queryWrapper2.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
            }
            if (ObjectUtil.isNotEmpty(params.getCondition().getEndDateS()) && params.getCondition().getSearchStatus() == 2) {
//                queryWrapper.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
                queryWrapper2.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
            }
        }
        //默认查询启用 (userId isNull 需要查询)
//        if (ObjectUtil.isNull(params.getCondition().getSearchStatus()) || params.getCondition().getSearchStatus() != 2) {
//            queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);
//            queryWrapper2.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);
//        } else {
//            queryWrapper.lambda().and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, DateUtil.date())
//                    .or().isNull(YkfCustomerSubAccountEntity::getUserId));
//            queryWrapper2.lambda().and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, DateUtil.date())
//                    .or().isNull(YkfCustomerSubAccountEntity::getUserId));
//        }

        if (ObjectUtil.isNull(params.getCondition().getSearchStatus())) {
            params.getCondition().setSearchStatus(1);
        }
        //默认查询启用 (userId isNull 需要查询)
        if (params.getCondition().getSearchStatus() == 1) {
            queryWrapper.lambda()
                    .eq(YkfCustomerSubAccountEntity::getStatus, 1)
                    .and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, DateUtil.date())
                    .or().isNull(YkfCustomerSubAccountEntity::getUserId));
            LambdaQueryWrapper<YkfCustomerSubAccountEntity> and = queryWrapper2.lambda()
                    .eq(YkfCustomerSubAccountEntity::getStatus, 1)
                    .and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, DateUtil.date())
                    .or().isNull(YkfCustomerSubAccountEntity::getUserId));
        }

//        //过滤用户权限
//        List<Long> powerUserIds = userService.getPowerUserIds();
//        if (ObjectUtil.isNotEmpty(powerUserIds)) {
//            queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getCreateUserId, powerUserIds);
//        }
        //过滤子账号
        List<Long> powerSubAccountIds = userService.getPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
                queryWrapper2.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
            }
        }

        queryWrapper.lambda().orderByDesc(YkfCustomerSubAccountEntity::getCustomerId, YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getSubAccountId);
        queryWrapper2.lambda().orderByDesc(YkfCustomerSubAccountEntity::getCustomerId, YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getSubAccountId);

        queryWrapper2.lambda().groupBy(YkfCustomerSubAccountEntity::getCustomerId);
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper2
        );
        if (ObjectUtil.isEmpty(page) || ObjectUtil.isEmpty(page.getRecords())) {
            return new PageUtils();
        }

        List<Long> collect1 = page.getRecords().stream().map(YkfCustomerSubAccountEntity::getCustomerId).collect(Collectors.toList());
        queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getCustomerId, collect1);

//        IPage<YkfCustomerSubAccountEntity> page = this.page(
//                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
//                queryWrapper
//        );
        List<YkfCustomerSubAccountEntity> list1 = this.list(queryWrapper);
        IPage<SubAccountModel> resultPage = new Query<SubAccountModel>().getPage(BeanUtil.beanToMap(params.getPage()));
        if (ObjectUtil.isNotEmpty(page)) {
            resultPage.setCurrent(page.getCurrent());
            resultPage.setSize(page.getSize());
            resultPage.setPages(page.getPages());
            resultPage.setTotal(page.getTotal());
            List<SubAccountModel> records = new ArrayList<>();
            List<SubAccountModel> sub1List = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(list1)) {

                Map<Long, List<YkfCustomerSubAccountEntity>> customerMap = list1.stream()
                        .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getCustomerId,LinkedHashMap::new,Collectors.toList()));

                for (List<YkfCustomerSubAccountEntity> value1 : customerMap.values()) {
                    SubAccountModel sub1 = new SubAccountModel();
                    sub1.setCustomerName(value1.get(0).getCustomerName());
                    List<SubAccountModel> sub2List = new ArrayList<>();
                    LinkedHashMap<Long, List<YkfCustomerSubAccountEntity>> shopMap = value1.stream().filter(o -> ObjectUtil.isNotEmpty(o.getShopId()))
                            .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getShopId, LinkedHashMap::new, Collectors.toList()));

                    for (List<YkfCustomerSubAccountEntity> value2 : shopMap.values()) {
                        SubAccountModel sub2 = new SubAccountModel();
                        YkfCustomerShopEntity shop = customerShopService.getById(value2.get(0).getShopId());
                        sub2.setShopName(value2.get(0).getShopName());
                        List<SubAccountModel> sub3List = new ArrayList<>();
                        LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> accountMap = value2.stream()
                                .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getAccountNo, LinkedHashMap::new, Collectors.toList()));

                        for (List<YkfCustomerSubAccountEntity> value3 : accountMap.values()) {
                            SubAccountModel sub3 = new SubAccountModel();
                            sub3.setAccountName(value3.get(0).getAccountName());
                            for (YkfCustomerSubAccountEntity subAccount : value3) {
                                subAccount.setCreateUserName(subAccount.getBranchDispatchName());
                                if (ObjectUtil.isEmpty(subAccount.getConfirmStatus())) {
                                    subAccount.setConfirmStatus(0);
                                }
                                if (ObjectUtil.isNotEmpty(shop)) {
                                    subAccount.setShopGroup(shop.getShopGroup());
                                    subAccount.setCustomType(shop.getCustomTypeNo());
                                    subAccount.setCustomTypeName(shop.getCustomTypeName());
                                    subAccount.setStatus(shop.getCooperateStatus());
                                    subAccount.setPlatformNo(shop.getPlatformNo());
                                    subAccount.setPlatformName(shop.getPlatformName());

                                    subAccount.setShopTypeName(shop.getShopTypeName());
                                    subAccount.setServiceShiftName(shop.getServiceShiftName());
                                    subAccount.setConsultPeopleNum(shop.getConsultPeopleNum());
                                    subAccount.setConversionRate(shop.getConversionRate());
                                    subAccount.setResponseSecond(shop.getResponseSecond());
                                    subAccount.setServiceShiftNo(shop.getServiceShiftNo());
                                    subAccount.setServiceShiftName(shop.getServiceShiftName());
                                }
                                if (ObjectUtil.isNotEmpty(subAccount.getUserId())) {
                                    SysUserEntity user = userService.getById(subAccount.getUserId());
                                    if (ObjectUtil.isNotEmpty(user)) {
                                        subAccount.setCustomPostType(user.getCustomPostType());
                                        subAccount.setCustomPostTypeName(user.getCustomPostTypeName());
                                    }
                                }
                                if (ObjectUtil.isNotEmpty(subAccount.getAllocationRuleId())) {
                                    YkfShopDistributeRuleEntity byId = ruleService.getById(subAccount.getAllocationRuleId());
                                    if (ObjectUtil.isNotEmpty(byId)) {
                                        subAccount.setAllocationRuleName(byId.getRuleName());
                                    }
                                }

                            }
                            sub3.setSubAccountList(value3);
                            sub3List.add(sub3);
                        }
                        sub2.setBodyList(sub3List);
                        sub2List.add(sub2);
                    }
                    sub1.setBodyList(sub2List);
                    sub1List.add(sub1);
                }

//                list1.stream().sorted(Comparator.comparing(YkfCustomerSubAccountEntity::getCreateTime).reversed());
//                LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> collect = new LinkedHashMap<>();
//                for (YkfCustomerSubAccountEntity subAccount : list1) {
//                    if (!collect.containsKey(StrUtil.toString(subAccount.getCustomerId()))) {
//                        List<YkfCustomerSubAccountEntity> list = new ArrayList<>();
//                        list.add(subAccount);
//                        collect.put(StrUtil.toString(subAccount.getCustomerId()), list);
//                    } else {
//                        List<YkfCustomerSubAccountEntity> list = collect.get(StrUtil.toString(subAccount.getCustomerId()));
//                        list.add(subAccount);
//                        collect.put(StrUtil.toString(subAccount.getCustomerId()), list);
//                    }
//                }
////                Map<String, List<YkfCustomerSubAccountEntity>> collect = list1.stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getShopId()))));
////                Map<String, List<YkfCustomerSubAccountEntity>> collect = list1.stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getCustomerId()))));
//                for (String key : collect.keySet()) {
//                    SubAccountModel subAccountModel = new SubAccountModel();
//                    List<YkfCustomerSubAccountEntity> list = collect.get(key);
////                    subAccountModel.setShopId(list.get(0).getShopId());
////                    subAccountModel.setShopName(list.get(0).getShopName());
//                    subAccountModel.setCustomerName(list.get(0).getCustomerName());
//                    subAccountModel.setPlatformName(list.get(0).getPlatformName());
//                    String accountNo = "";
//                    String shopId = "";
//                    YkfCustomerShopEntity shop = customerShopService.getById(list.get(0).getShopId());
//                    LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> map = new LinkedHashMap<>();
//                    for (YkfCustomerSubAccountEntity subAccount : list) {
//                        if (ObjectUtil.isNotEmpty(shop)) {
//                            subAccount.setShopGroup(shop.getShopGroup());
//                            subAccount.setCustomType(shop.getCustomTypeNo());
//                            subAccount.setCustomTypeName(shop.getCustomTypeName());
//                            subAccount.setStatus(shop.getCooperateStatus());
//                            subAccount.setPlatformNo(shop.getPlatformNo());
//                            subAccount.setPlatformName(shop.getPlatformName());
//                        }
//                        if (ObjectUtil.isNotEmpty(subAccount.getUserId())) {
//                            SysUserEntity user = userService.getById(subAccount.getUserId());
//                            if (ObjectUtil.isNotEmpty(user)) {
//                                subAccount.setCustomPostType(user.getCustomPostType());
//                                subAccount.setCustomPostTypeName(user.getCustomPostTypeName());
//                            }
//                        }
//                        if (ObjectUtil.isNotEmpty(subAccount.getAllocationRuleId())) {
//                            YkfShopDistributeRuleEntity byId = ruleService.getById(subAccount.getAllocationRuleId());
//                            if (ObjectUtil.isNotEmpty(byId)) {
//                                subAccount.setAllocationRuleName(byId.getRuleName());
//                            }
//                        }
//
////                        if (StrUtil.equals(subAccount.getAccountNo(), accountNo)) {
////                            subAccount.setAccountNo("");
////                            subAccount.setAccountName("");
////                        } else {
////                            accountNo = subAccount.getAccountNo();
////                        }
////
////                        if (StrUtil.equals(StrUtil.toString(subAccount.getShopId()), shopId)) {
////                            subAccount.setShopGroup("");
////                            subAccount.setCustomTypeName("");
////                            subAccount.setShopName("");
////                            subAccount.setPlatformName("");
////                        } else {
////                            shopId = StrUtil.toString(subAccount.getShopId());
////                        }
//                        if (map.containsKey(StrUtil.toString(subAccount.getShopId()))) {
//                            List<YkfCustomerSubAccountEntity> list2 = map.get(StrUtil.toString(subAccount.getShopId()));
//                            list2.add(subAccount);
//                            map.put(StrUtil.toString(subAccount.getShopId()), list2);
//                        } else {
//                            List<YkfCustomerSubAccountEntity> list2 = new ArrayList<>();
//                            list2.add(subAccount);
//                            map.put(StrUtil.toString(subAccount.getShopId()), list2);
//                        }
//                    }
////                    for (YkfCustomerSubAccountEntity item : list) {
////
////                    }
////                    list.sort(Comparator.comparing(YkfCustomerSubAccountEntity::getShopId).reversed());
//                    List<YkfCustomerSubAccountEntity> newList = new ArrayList<>();
//
//                    for (List<YkfCustomerSubAccountEntity> value : map.values()) {
//                        for (YkfCustomerSubAccountEntity subAccountEntity : value) {
//                            newList.add(subAccountEntity);
//                        }
//                    }
//
//                    for (YkfCustomerSubAccountEntity subAccount : newList) {
//                        if (StrUtil.equals(subAccount.getAccountNo(), accountNo)) {
//                            subAccount.setAccountNo("");
//                            subAccount.setAccountName("");
//                        } else {
//                            accountNo = subAccount.getAccountNo();
//                        }
//
//                        if (StrUtil.equals(StrUtil.toString(subAccount.getShopId()), shopId)) {
//                            subAccount.setShopGroup("");
//                            subAccount.setCustomTypeName("");
//                            subAccount.setShopName("");
//                            subAccount.setPlatformName("");
//                        } else {
//                            shopId = StrUtil.toString(subAccount.getShopId());
//                        }
//                    }
//                    subAccountModel.setSubAccountList(newList);
//                    modelList.add(subAccountModel);
//                }
            }
            resultPage.setRecords(sub1List);
        }
        return new PageUtils(resultPage);
    }

    @Override
    public PageUtils queryPage3(ParamsModel<YkfCustomerSubAccountEntity> params) {
//        if (ObjectUtil.isNull(params.getCondition().getSearchStatus()) || params.getCondition().getSearchStatus() != 2) {
//            if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
//                ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
//                sortParams.setSidx("updateTime");
//                sortParams.setOrder("desc");
//                params.setSort(sortParams);
//            }
//        } else {
//            if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
//                ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
//                sortParams.setSidx("startDate");
//                sortParams.setOrder("desc");
//                params.setSort(sortParams);
//            }
//        }
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("updateTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapper(params);
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper2 = ParamUtils.createQueryWrapper(params);
        queryWrapper.select("sub_account_id as subAccountId," +
                "account_no as accountNo," +
                "account_name as accountName," +
                "account_password as accountPassword," +
                "customer_id as customerId," +
                "customer_name as customerName," +
                "shop_id as shopId," +
                "shop_no as shopNo," +
                "shop_name as shopName," +
                "allocation_rule_id as allocationRuleId," +
                "allocation_rule_name as allocationRuleName," +
                "user_id as userId," +
                "flower_name as flowerName," +
                "name," +
                "custom_post_type as customPostType," +
                "custom_post_type_name as customPostTypeName," +
                "ex_name as exName," +
                "ex_name_label as exNameLabel," +
                "start_date as startDate," +
                "end_date as endDate," +
                "base_salary as baseSalary," +
                "convention_salary as conventionSalary," +
                "commission_ratio as commissionRatio," +
                "send_sms_sign as sendSmsSign," +
                "status," +
                "confirm_status as confirmStatus," +
                "on_line_status as onLineStatus," +
                "no," +
                "mobile," +
                "group_no as groupNo," +
                "platform_no as platformNo," +
                "platform_name as platformName," +
                "shop_link_url as shopLinkUrl," +
                "remark," +
                "create_user_id as createUserId," +
                "create_user_name as createUserName," +
                "create_time as createTime," +
                "old_id as oldId," +
                "old_sub_id as oldSubId," +
                "input_type as inputType," +
                "company_no as companyNo," +
                "company_name as companyName," +
                "company_base_salary as companyBaseSalary," +
                "stop_user_id as stopUserId," +
                "stop_user_name as stopUserName," +
                "stop_time as stopTime," +
                "update_user_id as updateUserId," +
                "update_user_name as updateUserName," +
                "update_time as updateTime," +
                "IF(user_id is null, 1 , 0) as defaultSort");
        queryWrapper2.select("sub_account_id as subAccountId," +
                "account_no as accountNo," +
                "account_name as accountName," +
                "account_password as accountPassword," +
                "customer_id as customerId," +
                "customer_name as customerName," +
                "shop_id as shopId," +
                "shop_no as shopNo," +
                "shop_name as shopName," +
                "allocation_rule_id as allocationRuleId," +
                "allocation_rule_name as allocationRuleName," +
                "user_id as userId," +
                "flower_name as flowerName," +
                "name," +
                "custom_post_type as customPostType," +
                "custom_post_type_name as customPostTypeName," +
                "ex_name as exName," +
                "ex_name_label as exNameLabel," +
                "start_date as startDate," +
                "end_date as endDate," +
                "base_salary as baseSalary," +
                "convention_salary as conventionSalary," +
                "commission_ratio as commissionRatio," +
                "send_sms_sign as sendSmsSign," +
                "status," +
                "confirm_status as confirmStatus," +
                "on_line_status as onLineStatus," +
                "no," +
                "mobile," +
                "group_no as groupNo," +
                "platform_no as platformNo," +
                "platform_name as platformName," +
                "shop_link_url as shopLinkUrl," +
                "remark," +
                "create_user_id as createUserId," +
                "create_user_name as createUserName," +
                "create_time as createTime," +
                "old_id as oldId," +
                "old_sub_id as oldSubId," +
                "input_type as inputType," +
                "company_no as companyNo," +
                "company_name as companyName," +
                "company_base_salary as companyBaseSalary," +
                "stop_user_id as stopUserId," +
                "stop_user_name as stopUserName," +
                "stop_time as stopTime," +
                "update_user_id as updateUserId," +
                "update_user_name as updateUserName," +
                "update_time as updateTime," +
                "IF(user_id is null, 1 , 0) as defaultSort");
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                String keyword = params.getCondition().getKeyword();
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
                queryWrapper2.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
            }
            if (ObjectUtil.isNotEmpty(params.getCondition().getEndDateS()) && params.getCondition().getSearchStatus() == 2) {
//                queryWrapper.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
                queryWrapper2.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
            }
        }
        //默认查询启用 (userId isNull 需要查询)
        queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);
        queryWrapper2.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);

//        //过滤用户权限
//        List<Long> powerUserIds = userService.getPowerUserIds();
//        if (ObjectUtil.isNotEmpty(powerUserIds)) {
//            queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getCreateUserId, powerUserIds);
//        }
        //过滤子账号
//        List<Long> powerSubAccountIds = userService.getPowerSubAccountIds();
        List<Long> powerSubAccountIds = userService.getOneselfPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
                queryWrapper2.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
            }
        }

        queryWrapper.orderByDesc("defaultSort");
        queryWrapper.orderBy(true, StrUtil.equals(params.getSort().getOrder(), "asc", true), StrUtil.toUnderlineCase(params.getSort().getSidx()));
        queryWrapper.lambda().orderByDesc(YkfCustomerSubAccountEntity::getCustomerId, YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getSubAccountId);

        queryWrapper2.orderByDesc("defaultSort");
        queryWrapper2.orderBy(true, StrUtil.equals(params.getSort().getOrder(), "asc", true), StrUtil.toUnderlineCase(params.getSort().getSidx()));
        queryWrapper2.lambda().orderByDesc(YkfCustomerSubAccountEntity::getCustomerId, YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getSubAccountId);

        queryWrapper2.lambda().groupBy(YkfCustomerSubAccountEntity::getCustomerId);
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper2
        );
        if (ObjectUtil.isEmpty(page) || ObjectUtil.isEmpty(page.getRecords())) {
            return new PageUtils();
        }

        List<Long> collect1 = page.getRecords().stream().map(YkfCustomerSubAccountEntity::getCustomerId).collect(Collectors.toList());
        queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getCustomerId, collect1)
                .groupBy(YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getAccountNo);

//        IPage<YkfCustomerSubAccountEntity> page = this.page(
//                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
//                queryWrapper
//        );
        List<YkfCustomerSubAccountEntity> list1 = this.list(queryWrapper);

        IPage<SubAccountModel> resultPage = new Query<SubAccountModel>().getPage(BeanUtil.beanToMap(params.getPage()));
        if (ObjectUtil.isNotEmpty(page)) {

            resultPage.setCurrent(page.getCurrent());
            resultPage.setSize(page.getSize());
            resultPage.setPages(page.getPages());
            resultPage.setTotal(page.getTotal());
            List<SubAccountModel> records = new ArrayList<>();
            List<SubAccountModel> sub1List = new ArrayList<>();

            if (ObjectUtil.isNotEmpty(list1)) {

//                List<SubAccountModel> sub1List = new ArrayList<>();
                Map<Long, List<YkfCustomerSubAccountEntity>> customerMap = list1.stream()
                        .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getCustomerId,LinkedHashMap::new,Collectors.toList()));

                for (List<YkfCustomerSubAccountEntity> value1 : customerMap.values()) {
                    SubAccountModel sub1 = new SubAccountModel();
                    sub1.setCustomerName(value1.get(0).getCustomerName());
                    List<SubAccountModel> sub2List = new ArrayList<>();
                    LinkedHashMap<Long, List<YkfCustomerSubAccountEntity>> shopMap = value1.stream().filter(o -> ObjectUtil.isNotEmpty(o.getShopId()))
                            .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getShopId, LinkedHashMap::new, Collectors.toList()));

                    for (List<YkfCustomerSubAccountEntity> value2 : shopMap.values()) {
                        SubAccountModel sub2 = new SubAccountModel();
                        YkfCustomerShopEntity shop = customerShopService.getById(value2.get(0).getShopId());
                        sub2.setShopName(value2.get(0).getShopName());
                        List<SubAccountModel> sub3List = new ArrayList<>();
                        LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> accountMap = value2.stream()
                                .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getAccountNo, LinkedHashMap::new, Collectors.toList()));

                        for (List<YkfCustomerSubAccountEntity> value3 : accountMap.values()) {
                            SubAccountModel sub3 = new SubAccountModel();
                            sub3.setAccountName(value3.get(0).getAccountName());
                            for (YkfCustomerSubAccountEntity subAccount : value3) {
                                if (ObjectUtil.isNotEmpty(shop)) {
                                    if (ObjectUtil.isEmpty(subAccount.getConfirmStatus())) {
                                        subAccount.setConfirmStatus(0);
                                    }
                                    subAccount.setShopGroup(shop.getShopGroup());
                                    subAccount.setCustomType(shop.getCustomTypeNo());
                                    subAccount.setCustomTypeName(shop.getCustomTypeName());
                                    subAccount.setStatus(shop.getCooperateStatus());
                                    subAccount.setPlatformNo(shop.getPlatformNo());
                                    subAccount.setPlatformName(shop.getPlatformName());

                                    subAccount.setShopTypeName(shop.getShopTypeName());
                                    subAccount.setServiceShiftName(shop.getServiceShiftName());
                                    subAccount.setConsultPeopleNum(shop.getConsultPeopleNum());
                                    subAccount.setConversionRate(shop.getConversionRate());
                                    subAccount.setResponseSecond(shop.getResponseSecond());
                                    subAccount.setServiceShiftNo(shop.getServiceShiftNo());
                                    subAccount.setServiceShiftName(shop.getServiceShiftName());
                                }
                                if (ObjectUtil.isNotEmpty(subAccount.getUserId())) {
                                    SysUserEntity user = userService.getById(subAccount.getUserId());
                                    if (ObjectUtil.isNotEmpty(user)) {
                                        subAccount.setCustomPostType(user.getCustomPostType());
                                        subAccount.setCustomPostTypeName(user.getCustomPostTypeName());
                                    }
                                }
                                if (ObjectUtil.isNotEmpty(subAccount.getAllocationRuleId())) {
                                    YkfShopDistributeRuleEntity byId = ruleService.getById(subAccount.getAllocationRuleId());
                                    if (ObjectUtil.isNotEmpty(byId)) {
                                        subAccount.setAllocationRuleName(byId.getRuleName());
                                    }
                                }

                            }
                            sub3.setSubAccountList(value3);
                            sub3List.add(sub3);
                        }
                        sub2.setBodyList(sub3List);
                        sub2List.add(sub2);
                    }
                    sub1.setBodyList(sub2List);
                    sub1List.add(sub1);
                }

//                list1.stream().sorted(Comparator.comparing(YkfCustomerSubAccountEntity::getCreateTime).reversed());
//                LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> collect = new LinkedHashMap<>();
//                for (YkfCustomerSubAccountEntity subAccount : list1) {
//                    if (!collect.containsKey(StrUtil.toString(subAccount.getCustomerId()))) {
//                        List<YkfCustomerSubAccountEntity> list = new ArrayList<>();
//                        list.add(subAccount);
//                        collect.put(StrUtil.toString(subAccount.getCustomerId()), list);
//                    } else {
//                        List<YkfCustomerSubAccountEntity> list = collect.get(StrUtil.toString(subAccount.getCustomerId()));
//                        list.add(subAccount);
//                        collect.put(StrUtil.toString(subAccount.getCustomerId()), list);
//                    }
//                }
////                Map<String, List<YkfCustomerSubAccountEntity>> collect = list1.stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getShopId()))));
////                Map<String, List<YkfCustomerSubAccountEntity>> collect = list1.stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getCustomerId()))));
//                for (String key : collect.keySet()) {
//                    SubAccountModel subAccountModel = new SubAccountModel();
//                    List<YkfCustomerSubAccountEntity> list = collect.get(key);
////                    subAccountModel.setShopId(list.get(0).getShopId());
////                    subAccountModel.setShopName(list.get(0).getShopName());
//                    subAccountModel.setCustomerName(list.get(0).getCustomerName());
//                    subAccountModel.setPlatformName(list.get(0).getPlatformName());
//                    String accountNo = "";
//                    String shopId = "";
//                    YkfCustomerShopEntity shop = customerShopService.getById(list.get(0).getShopId());
//                    LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> map = new LinkedHashMap<>();
//                    for (YkfCustomerSubAccountEntity subAccount : list) {
//                        if (ObjectUtil.isNotEmpty(shop)) {
//                            subAccount.setShopGroup(shop.getShopGroup());
//                            subAccount.setCustomType(shop.getCustomTypeNo());
//                            subAccount.setCustomTypeName(shop.getCustomTypeName());
//                            subAccount.setStatus(shop.getCooperateStatus());
//                            subAccount.setPlatformNo(shop.getPlatformNo());
//                            subAccount.setPlatformName(shop.getPlatformName());
//                        }
//                        if (ObjectUtil.isNotEmpty(subAccount.getUserId())) {
//                            SysUserEntity user = userService.getById(subAccount.getUserId());
//                            if (ObjectUtil.isNotEmpty(user)) {
//                                subAccount.setCustomPostType(user.getCustomPostType());
//                                subAccount.setCustomPostTypeName(user.getCustomPostTypeName());
//                            }
//                        }
//                        if (ObjectUtil.isNotEmpty(subAccount.getAllocationRuleId())) {
//                            YkfShopDistributeRuleEntity byId = ruleService.getById(subAccount.getAllocationRuleId());
//                            if (ObjectUtil.isNotEmpty(byId)) {
//                                subAccount.setAllocationRuleName(byId.getRuleName());
//                            }
//                        }
//                        if (map.containsKey(StrUtil.toString(subAccount.getShopId()))) {
//                            List<YkfCustomerSubAccountEntity> list2 = map.get(StrUtil.toString(subAccount.getShopId()));
//                            list2.add(subAccount);
//                            map.put(StrUtil.toString(subAccount.getShopId()), list2);
//                        } else {
//                            List<YkfCustomerSubAccountEntity> list2 = new ArrayList<>();
//                            list2.add(subAccount);
//                            map.put(StrUtil.toString(subAccount.getShopId()), list2);
//                        }
//                    }
////                    for (YkfCustomerSubAccountEntity item : list) {
////
////                    }
////                    list.sort(Comparator.comparing(YkfCustomerSubAccountEntity::getShopId).reversed());
//                    List<YkfCustomerSubAccountEntity> newList = new ArrayList<>();
//
//                    for (List<YkfCustomerSubAccountEntity> value : map.values()) {
//                        for (YkfCustomerSubAccountEntity subAccountEntity : value) {
//                            newList.add(subAccountEntity);
//                        }
//                    }
////                    for (YkfCustomerSubAccountEntity subAccount : newList) {
////                        if (StrUtil.equals(subAccount.getAccountNo(), accountNo)) {
////                            subAccount.setAccountNo("");
////                            subAccount.setAccountName("");
////                        } else {
////                            accountNo = subAccount.getAccountNo();
////                        }
////
////                        if (StrUtil.equals(StrUtil.toString(subAccount.getShopId()), shopId)) {
////                            subAccount.setShopGroup("");
////                            subAccount.setCustomTypeName("");
////                            subAccount.setShopName("");
////                            subAccount.setPlatformName("");
////                        } else {
////                            shopId = StrUtil.toString(subAccount.getShopId());
////                        }
////                    }
//                    subAccountModel.setSubAccountList(newList);
//                    modelList.add(subAccountModel);
//                }
            }
            resultPage.setRecords(sub1List);
        }
        return new PageUtils(resultPage);
    }

    /**
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils sumSubAccount(ParamsModel<SumSubAccountModel> params) {
        if (null == params.getCondition()) {
            params.setCondition(new SumSubAccountModel());
        }
        params.getCondition().setNow(DateUtil.parseDate(DateUtil.formatDate(DateUtil.date())));
        IPage<SumSubAccountModel> queryPage = new Query<SumSubAccountModel>().getPage(BeanUtil.beanToMap(params.getPage()));
        IPage<SumSubAccountModel> page = baseMapper.sumSubAccount(
                queryPage, params.getCondition(), params.getSort()
        );
        return new PageUtils(page);
    }

    @Override
    public void storeAllocation(ExcelReq req, HttpServletResponse response) throws IOException {
        //查询列表
        ParamsModel<SumSubAccountModel> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<SumSubAccountModel>() {
        }));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = sumSubAccount(params);
        List<SumSubAccountModel> dataList = (List<SumSubAccountModel>) pageUtils.getList();
        List<StoreAllocationExcel> excelList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(dataList)) {
            for (SumSubAccountModel item : dataList) {
                StoreAllocationExcel excel = new StoreAllocationExcel();
                BeanUtil.copyProperties(item, excel);
                excelList.add(excel);
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("店铺分配表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            EasyExcel.write(response.getOutputStream(), StoreAllocationExcel.class)
                    .sheet("店铺分配表")
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(widthStyleStrategy)
                    .doWrite(excelList);
        }
    }

    @Override
    public PageUtils queryPage2(ParamsModel<YkfCustomerSubAccountEntity> params) {
        if (ObjectUtil.isNull(params.getCondition().getSearchStatus()) || params.getCondition().getSearchStatus() != 2) {
            if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
                ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
                sortParams.setSidx("startDate");
                sortParams.setOrder("asc");
                params.setSort(sortParams);
            }
        } else {
            if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
                ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
                sortParams.setSidx("startDate");
                sortParams.setOrder("asc");
                params.setSort(sortParams);
            }
        }
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper2 = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                String keyword = params.getCondition().getKeyword();
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
                queryWrapper2.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
            }
            if (ObjectUtil.isNotEmpty(params.getCondition().getEndDateS()) && params.getCondition().getSearchStatus() == 2) {
//                queryWrapper.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
                queryWrapper.lambda().and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS()).or().isNull(YkfCustomerSubAccountEntity::getEndDate));
                queryWrapper2.lambda().and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS()).or().isNull(YkfCustomerSubAccountEntity::getEndDate));
            }
        }
        if (ObjectUtil.isNull(params.getCondition().getSearchStatus())) {
            params.getCondition().setSearchStatus(1);
        }
        //默认查询启用 (userId isNull 需要查询)
        if (params.getCondition().getSearchStatus() == 1) {
            queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1)
                    .and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, DateUtil.date())
                    .or().isNull(YkfCustomerSubAccountEntity::getUserId));
            queryWrapper2.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1)
                    .and(wrapper -> wrapper.ge(YkfCustomerSubAccountEntity::getEndDate, DateUtil.date())
                    .or().isNull(YkfCustomerSubAccountEntity::getUserId));
        }
//        else {
//            queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);
//            queryWrapper2.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);
//        }

//        //过滤用户权限
//        List<Long> powerUserIds = userService.getPowerUserIds();
//        if (ObjectUtil.isNotEmpty(powerUserIds)) {
//            queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getCreateUserId, powerUserIds);
//        }
        //过滤子账号
        List<Long> powerSubAccountIds = userService.getPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
                queryWrapper2.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
            }
        }

        queryWrapper.lambda().orderByDesc(YkfCustomerSubAccountEntity::getCustomerId, YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getSubAccountId);
        queryWrapper2.lambda().orderByDesc(YkfCustomerSubAccountEntity::getCustomerId, YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getSubAccountId);

        queryWrapper2.lambda().groupBy(YkfCustomerSubAccountEntity::getCustomerId);
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper2
        );
        if (ObjectUtil.isEmpty(page) || ObjectUtil.isEmpty(page.getRecords())) {
            return new PageUtils();
        }

        List<Long> collect1 = page.getRecords().stream().map(YkfCustomerSubAccountEntity::getCustomerId).collect(Collectors.toList());
        queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getCustomerId, collect1);

//        IPage<YkfCustomerSubAccountEntity> page = this.page(
//                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
//                queryWrapper
//        );
        List<YkfCustomerSubAccountEntity> list1 = this.list(queryWrapper);
        IPage<SubAccountModel> resultPage = new Query<SubAccountModel>().getPage(BeanUtil.beanToMap(params.getPage()));

        boolean masterCompany = userService.isMasterCompany();
        if (ObjectUtil.isNotEmpty(page)) {
            resultPage.setCurrent(page.getCurrent());
            resultPage.setSize(page.getSize());
            resultPage.setPages(page.getPages());
            resultPage.setTotal(page.getTotal());
            List<SubAccountModel> records = new ArrayList<>();
            List<SubAccountModel> sub1List = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(list1)) {

                Map<Long, List<YkfCustomerSubAccountEntity>> customerMap = list1.stream()
                        .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getCustomerId,LinkedHashMap::new,Collectors.toList()));

                for (List<YkfCustomerSubAccountEntity> value1 : customerMap.values()) {
                    SubAccountModel sub1 = new SubAccountModel();
                    sub1.setCustomerName(value1.get(0).getCustomerName());
                    List<SubAccountModel> sub2List = new ArrayList<>();
                    LinkedHashMap<Long, List<YkfCustomerSubAccountEntity>> shopMap = value1.stream().filter(o -> ObjectUtil.isNotEmpty(o.getShopId()))
                            .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getShopId, LinkedHashMap::new, Collectors.toList()));

                    for (List<YkfCustomerSubAccountEntity> value2 : shopMap.values()) {
                        SubAccountModel sub2 = new SubAccountModel();
                        YkfCustomerShopEntity shop = customerShopService.getById(value2.get(0).getShopId());
                        sub2.setShopName(value2.get(0).getShopName());
                        List<SubAccountModel> sub3List = new ArrayList<>();
                        LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> accountMap = value2.stream()
                                .collect(Collectors.groupingBy(YkfCustomerSubAccountEntity::getAccountNo, LinkedHashMap::new, Collectors.toList()));

                        for (List<YkfCustomerSubAccountEntity> value3 : accountMap.values()) {
                            SubAccountModel sub3 = new SubAccountModel();
                            sub3.setAccountName(value3.get(0).getAccountName());
                            for (int i = 0; i < value3.size(); i++) {
                                YkfCustomerSubAccountEntity subAccount = value3.get(i);
                                if (masterCompany) {
                                    subAccount.setCreateUserName(subAccount.getHeadDispatchName());
                                } else {
                                    subAccount.setCreateUserName(subAccount.getBranchDispatchName());
                                }
                                if (ObjectUtil.isEmpty(subAccount.getConfirmStatus())) {
                                    subAccount.setConfirmStatus(0);
                                }
                                if (i != 0) {
                                    subAccount.setAccountNo("");
                                    subAccount.setAccountName("");
                                }
                                if (ObjectUtil.isNotEmpty(shop)) {
                                    subAccount.setBaseSalary(shop.getBaseSalary());
                                    subAccount.setShopGroup(shop.getShopGroup());
                                    subAccount.setCustomType(shop.getCustomTypeNo());
                                    subAccount.setCustomTypeName(shop.getCustomTypeName());
                                    subAccount.setSubStatus(subAccount.getStatus());
                                    subAccount.setStatus(shop.getCooperateStatus());
                                    subAccount.setPlatformNo(shop.getPlatformNo());
                                    subAccount.setPlatformName(shop.getPlatformName());

                                    subAccount.setShopTypeName(shop.getShopTypeName());
                                    subAccount.setServiceShiftName(shop.getServiceShiftName());
                                    subAccount.setConsultPeopleNum(shop.getConsultPeopleNum());
                                    subAccount.setConversionRate(shop.getConversionRate());
                                    subAccount.setResponseSecond(shop.getResponseSecond());
                                    subAccount.setCommission(shop.getCommission());
                                    subAccount.setServiceShiftNo(shop.getServiceShiftNo());
                                    subAccount.setServiceShiftName(shop.getServiceShiftName());
                                }
                                if (ObjectUtil.isNotEmpty(subAccount.getUserId())) {
                                    SysUserEntity user = userService.getById(subAccount.getUserId());
                                    if (ObjectUtil.isNotEmpty(user)) {
                                        subAccount.setCustomPostType(user.getCustomPostType());
                                        subAccount.setCustomPostTypeName(user.getCustomPostTypeName());
                                    }
                                }
                                if (ObjectUtil.isNotEmpty(subAccount.getAllocationRuleId())) {
                                    YkfShopDistributeRuleEntity byId = ruleService.getById(subAccount.getAllocationRuleId());
                                    if (ObjectUtil.isNotEmpty(byId)) {
                                        subAccount.setAllocationRuleName(byId.getRuleName());
                                    }
                                }
                                if (masterCompany) {
                                    subAccount.setCompanyBaseSalary(subAccount.getCompanyBaseSalary());
                                } else {
                                    subAccount.setCompanyBaseSalary(subAccount.getConventionSalary());
                                }
                            }
                            sub3.setSubAccountList(value3);
                            sub3List.add(sub3);
                        }
                        sub2.setBodyList(sub3List);
                        sub2List.add(sub2);
                    }
                    sub1.setBodyList(sub2List);
                    sub1List.add(sub1);
                }
//                list1.stream().sorted(Comparator.comparing(YkfCustomerSubAccountEntity::getCreateTime).reversed());
//                LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> collect = new LinkedHashMap<>();
//                for (YkfCustomerSubAccountEntity subAccount : list1) {
//                    if (!collect.containsKey(StrUtil.toString(subAccount.getCustomerId()))) {
//                        List<YkfCustomerSubAccountEntity> list = new ArrayList<>();
//                        list.add(subAccount);
//                        collect.put(StrUtil.toString(subAccount.getCustomerId()), list);
//                    } else {
//                        List<YkfCustomerSubAccountEntity> list = collect.get(StrUtil.toString(subAccount.getCustomerId()));
//                        list.add(subAccount);
//                        collect.put(StrUtil.toString(subAccount.getCustomerId()), list);
//                    }
//                }
////                Map<String, List<YkfCustomerSubAccountEntity>> collect = list1.stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getShopId()))));
////                Map<String, List<YkfCustomerSubAccountEntity>> collect = list1.stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getCustomerId()))));
//                for (String key : collect.keySet()) {
//                    SubAccountModel subAccountModel = new SubAccountModel();
//                    List<YkfCustomerSubAccountEntity> list = collect.get(key);
////                    subAccountModel.setShopId(list.get(0).getShopId());
////                    subAccountModel.setShopName(list.get(0).getShopName());
//                    subAccountModel.setCustomerName(list.get(0).getCustomerName());
//                    subAccountModel.setPlatformName(list.get(0).getPlatformName());
//                    String accountNo = "";
//                    String shopId = "";
//                    YkfCustomerShopEntity shop = customerShopService.getById(list.get(0).getShopId());
//                    LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> map = new LinkedHashMap<>();
//                    for (YkfCustomerSubAccountEntity subAccount : list) {
//                        if (ObjectUtil.isNotEmpty(shop)) {
//                            subAccount.setShopGroup(shop.getShopGroup());
//                            subAccount.setCustomType(shop.getCustomTypeNo());
//                            subAccount.setCustomTypeName(shop.getCustomTypeName());
//                            subAccount.setStatus(shop.getCooperateStatus());
//                            subAccount.setPlatformNo(shop.getPlatformNo());
//                            subAccount.setPlatformName(shop.getPlatformName());
//                        }
//                        if (ObjectUtil.isNotEmpty(subAccount.getUserId())) {
//                            SysUserEntity user = userService.getById(subAccount.getUserId());
//                            if (ObjectUtil.isNotEmpty(user)) {
//                                subAccount.setCustomPostType(user.getCustomPostType());
//                                subAccount.setCustomPostTypeName(user.getCustomPostTypeName());
//                            }
//                        }
//                        if (ObjectUtil.isNotEmpty(subAccount.getAllocationRuleId())) {
//                            YkfShopDistributeRuleEntity byId = ruleService.getById(subAccount.getAllocationRuleId());
//                            if (ObjectUtil.isNotEmpty(byId)) {
//                                subAccount.setAllocationRuleName(byId.getRuleName());
//                            }
//                        }
//
//                        if (masterCompany) {
//                            subAccount.setCompanyBaseSalary(subAccount.getCompanyBaseSalary());
//                        } else {
//                            subAccount.setCompanyBaseSalary(subAccount.getConventionSalary());
//                        }
//                        if (map.containsKey(StrUtil.toString(subAccount.getShopId()))) {
//                            List<YkfCustomerSubAccountEntity> list2 = map.get(StrUtil.toString(subAccount.getShopId()));
//                            list2.add(subAccount);
//                            map.put(StrUtil.toString(subAccount.getShopId()), list2);
//                        } else {
//                            List<YkfCustomerSubAccountEntity> list2 = new ArrayList<>();
//                            list2.add(subAccount);
//                            map.put(StrUtil.toString(subAccount.getShopId()), list2);
//                        }
//                    }
////                    for (YkfCustomerSubAccountEntity item : list) {
////
////                    }
////                    list.sort(Comparator.comparing(YkfCustomerSubAccountEntity::getShopId).reversed());
//                    List<YkfCustomerSubAccountEntity> newList = new ArrayList<>();
//
//                    for (List<YkfCustomerSubAccountEntity> value : map.values()) {
//                        for (YkfCustomerSubAccountEntity subAccountEntity : value) {
//                            newList.add(subAccountEntity);
//                        }
//                    }
//                    for (YkfCustomerSubAccountEntity subAccount : newList) {
//                        if (StrUtil.equals(subAccount.getAccountNo(), accountNo)) {
//                            subAccount.setAccountNo("");
//                            subAccount.setAccountName("");
//                        } else {
//                            accountNo = subAccount.getAccountNo();
//                        }
//
//                        if (StrUtil.equals(StrUtil.toString(subAccount.getShopId()), shopId)) {
//                            subAccount.setShopGroup("");
//                            subAccount.setCustomTypeName("");
//                            subAccount.setShopName("");
//                            subAccount.setPlatformName("");
//                        } else {
//                            shopId = StrUtil.toString(subAccount.getShopId());
//                        }
//                    }
//                    subAccountModel.setSubAccountList(newList);
//                    modelList.add(subAccountModel);
//                }
            }
            resultPage.setRecords(sub1List);
        }
        return new PageUtils(resultPage);
    }

    @Override
    public PageUtils distributeCusSerPage(ParamsModel<YkfCustomerSubAccountEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("createTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperBySort(params);
        IPage<YkfCustomerSubAccountEntity> page = new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage()));
        YkfCustomerSubAccountEntity condition = params.getCondition();
        if (ObjectUtil.isNotEmpty(condition)) {
            if (StrUtil.isNotBlank(condition.getKeyword())) {
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, condition.getKeyword())
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, condition.getKeyword())
                        .or().like(YkfCustomerSubAccountEntity::getFlowerName, condition.getKeyword())
                        .or().like(YkfCustomerSubAccountEntity::getName, condition.getKeyword())
                        .or().like(YkfCustomerSubAccountEntity::getNo, condition.getKeyword())
                        .or().like(YkfCustomerSubAccountEntity::getGroupNo, condition.getKeyword())
                );
            }
            if (StrUtil.isNotBlank(condition.getAccountName())) {
                queryWrapper.lambda().like(YkfCustomerSubAccountEntity::getAccountName, condition.getAccountName());
            }
            if (StrUtil.isNotBlank(condition.getShopName())) {
                queryWrapper.lambda().like(YkfCustomerSubAccountEntity::getShopName, condition.getShopName());
            }
            if (ObjectUtil.isNotEmpty(condition.getStartDate()) && ObjectUtil.isNotEmpty(condition.getEndDate())) {
                queryWrapper.lambda().and(wrapper -> wrapper.le(YkfCustomerSubAccountEntity::getStartDate, condition.getEndDate())
                        .or().ge(YkfCustomerSubAccountEntity::getEndDate, condition.getStartDate()));
            }
            if (ObjectUtil.isNotEmpty(condition.getConfirmStatus())) {
                queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getConfirmStatus, condition.getConfirmStatus());
            }
        }
        List<Long> powerSubAccountIds = userService.getPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
            }
        }
        queryWrapper.lambda().isNotNull(YkfCustomerSubAccountEntity::getUserId);
        IPage<YkfCustomerSubAccountEntity> page1 = this.page(page, queryWrapper);
        boolean masterCompany = sysUserService.isMasterCompany();
        if (masterCompany && ObjectUtil.isNotEmpty(page1) && ObjectUtil.isNotEmpty(page1.getRecords())) {
            for (YkfCustomerSubAccountEntity record : page1.getRecords()) {
                record.setConventionSalary(record.getCompanyBaseSalary());
            }
        }
        return new PageUtils(page1);
    }

    @Override
    public PageUtils stopPage(ParamsModel<YkfCustomerSubAccountEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("stopTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                String keyword = params.getCondition().getKeyword();
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
            }
            if (ObjectUtil.isNotEmpty(params.getCondition().getEndDateS())) {
                queryWrapper.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
            }
        }
        //默认查询启用
        queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 2)
                .orderByAsc(YkfCustomerSubAccountEntity::getCreateTime)
                .groupBy(YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getAccountNo);
        //过滤权限
        List<Long> powerSubAccountIds = userService.getOneselfPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
            }
        }
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        IPage<SubAccountModel> resultPage = new Query<SubAccountModel>().getPage(BeanUtil.beanToMap(params.getPage()));
        if (ObjectUtil.isNotEmpty(page)) {
            resultPage.setCurrent(page.getCurrent());
            resultPage.setSize(page.getSize());
            resultPage.setPages(page.getPages());
            resultPage.setTotal(page.getTotal());
            List<SubAccountModel> records = new ArrayList<>();
            List<SubAccountModel> modelList = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(page.getRecords())) {
                LinkedHashMap<String, List<YkfCustomerSubAccountEntity>> collect = new LinkedHashMap<>();
                for (YkfCustomerSubAccountEntity record : page.getRecords()) {
                    if (collect.containsKey(StrUtil.toString(record.getShopId()))) {
                        List<YkfCustomerSubAccountEntity> list = collect.get(StrUtil.toString(record.getShopId()));
                        list.add(record);
                        collect.put(StrUtil.toString(record.getShopId()), list);
                    } else {
                        List<YkfCustomerSubAccountEntity> list = new ArrayList<>();
                        list.add(record);
                        collect.put(StrUtil.toString(record.getShopId()), list);
                    }
                }
//                Map<String, List<YkfCustomerSubAccountEntity>> collect = page.getRecords().stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getShopId()))));
                for (String key : collect.keySet()) {
                    SubAccountModel subAccountModel = new SubAccountModel();
                    List<YkfCustomerSubAccountEntity> list = collect.get(key);
                    subAccountModel.setShopId(list.get(0).getShopId());
                    subAccountModel.setShopName(list.get(0).getShopName());
                    subAccountModel.setCustomerName(list.get(0).getCustomerName());
                    subAccountModel.setPlatformName(list.get(0).getPlatformName());
                    subAccountModel.setSubAccountList(list);
                    modelList.add(subAccountModel);
                }
            }
            resultPage.setRecords(modelList);
        }
        return new PageUtils(resultPage);
    }


    @Override
    public PageUtils queryPage4(ParamsModel<YkfCustomerSubAccountEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("createTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (ObjectUtil.isNotEmpty(params.getCondition().getShopId())) {
                queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getShopId, params.getCondition().getShopId());
            }
        }
        //默认查询启用
        queryWrapper.lambda().groupBy(YkfCustomerSubAccountEntity::getShopId, YkfCustomerSubAccountEntity::getAccountNo);
        //过滤权限
        List<Long> powerSubAccountIds = userService.getOneselfPowerSubAccountIds();
        if (ObjectUtil.isEmpty(powerSubAccountIds)) {
            return new PageUtils();
        } else {
            if (!powerSubAccountIds.contains(0L)) {
                queryWrapper.lambda().in(YkfCustomerSubAccountEntity::getSubAccountId, powerSubAccountIds);
            }
        }
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 子账号在线列表
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils onLinePage(ParamsModel<YkfCustomerSubAccountEntity> params) {
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                String keyword = params.getCondition().getKeyword();
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getFlowerName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getName, keyword)
                );
            }
            if (ObjectUtil.isNotEmpty(params.getCondition().getEndDateS())) {
                queryWrapper.lambda().ge(YkfCustomerSubAccountEntity::getEndDate, params.getCondition().getEndDateS());
            }
        }
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        //默认查询启用
        queryWrapper.lambda().eq(YkfCustomerSubAccountEntity::getStatus, 1);
        IPage<SubAccountModel> resultPage = new Query<SubAccountModel>().getPage(BeanUtil.beanToMap(params.getPage()));
        if (ObjectUtil.isNotEmpty(page)) {
            resultPage.setCurrent(page.getCurrent());
            resultPage.setSize(page.getSize());
            resultPage.setPages(page.getPages());
            resultPage.setTotal(page.getTotal());
            List<SubAccountModel> records = new ArrayList<>();
            List<SubAccountModel> modelList = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(page.getRecords())) {
                Map<String, List<YkfCustomerSubAccountEntity>> collect = page.getRecords().stream().collect(Collectors.groupingBy(o -> StrUtil.toString(StrUtil.toString(o.getShopId()))));
                for (String key : collect.keySet()) {
                    SubAccountModel subAccountModel = new SubAccountModel();
                    List<YkfCustomerSubAccountEntity> list = collect.get(key);
                    subAccountModel.setShopId(list.get(0).getShopId());
                    subAccountModel.setShopName(list.get(0).getShopName());
                    subAccountModel.setCustomerName(list.get(0).getCustomerName());
                    subAccountModel.setPlatformName(list.get(0).getPlatformName());
                    subAccountModel.setSubAccountList(list);
                    modelList.add(subAccountModel);
                }
            }
            resultPage.setRecords(modelList);
        }
        return new PageUtils(resultPage);
    }

    @Override
    public PageUtils selectPage(ParamsModel<YkfCustomerSubAccountEntity> params) {
        QueryWrapper<YkfCustomerSubAccountEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition())) {
            if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
                String keyword = params.getCondition().getKeyword();
                queryWrapper.lambda().and(wrapper -> wrapper.like(YkfCustomerSubAccountEntity::getAccountNo, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getAccountName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getShopName, keyword)
                        .or().like(YkfCustomerSubAccountEntity::getCustomerName, keyword)
                );
            }
        }
        IPage<YkfCustomerSubAccountEntity> page = this.page(
                new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils personalShopPage(ParamsModel<YkfCustomerSubAccountEntity> params) {
        IPage<YkfCustomerSubAccountEntity> page = new Query<YkfCustomerSubAccountEntity>().getPage(BeanUtil.beanToMap(params.getPage()));
        IPage<YkfCustomerSubAccountEntity> resultPage = baseMapper.getUserShopPage(page, params.getCondition());
        if (ObjectUtil.isNotEmpty(resultPage) && ObjectUtil.isNotEmpty(resultPage.getRecords())) {
            DateTime date = DateUtil.date();
            for (YkfCustomerSubAccountEntity record : resultPage.getRecords()) {
                if (record.getStatus() == 1) {
                    if (date.compareTo(record.getStartDate()) < 0) {
                        record.setServiceStatus("0");
                    } else if (date.compareTo(record.getEndDate()) > 0) {
                        record.setServiceStatus("2");
                    } else {
                        record.setServiceStatus("1");
                    }
                } else {
                    record.setServiceStatus("2");
                }
            }
        }
        return new PageUtils(resultPage);
    }

    @Override
    public List<YkfCustomerSubAccountEntity> personalShopList() {
        List<YkfCustomerSubAccountEntity> list = baseMapper.getUserShopList(CommonUtils.getUserId());
        if (ObjectUtil.isNotEmpty(list)) {
            DateTime date = DateUtil.date();
            for (YkfCustomerSubAccountEntity record : list) {
                if (record.getStatus() == 1) {
                    if (date.compareTo(record.getStartDate()) < 0) {
                        record.setServiceStatus("0");
                    } else if (date.compareTo(record.getEndDate()) > 0) {
                        record.setServiceStatus("2");
                    } else {
                        record.setServiceStatus("1");
                    }
                } else {
                    record.setServiceStatus("2");
                }
            }
        }
        return list;
    }

    @Override
    public void confirmSubAccount(YkfCustomerSubAccountEntity subAccount) {
        if (ObjectUtil.isEmpty(subAccount) || ObjectUtil.isEmpty(subAccount.getSubAccountId())) {
            throw new RRException("参数缺失");
        }
        UpdateWrapper<YkfCustomerSubAccountEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(YkfCustomerSubAccountEntity::getSubAccountId, subAccount.getSubAccountId())
                .set(YkfCustomerSubAccountEntity::getConfirmStatus, 1);
        this.update(new YkfCustomerSubAccountEntity(), updateWrapper);
    }

    @Override
    public void subAccountManagement(ExcelReq req, HttpServletResponse response) throws IOException {
        //查询列表
        ParamsModel<YkfCustomerSubAccountEntity> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<YkfCustomerSubAccountEntity>() {
        }));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = queryPage(params);
        List<SubAccountModel> dataList = (List<SubAccountModel>) pageUtils.getList();
        List<YkfSubAccountManageExcel> excelList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(dataList)) {
            for (SubAccountModel item : dataList) {
                List<YkfCustomerSubAccountEntity> subAccountList = item.getSubAccountList();
                if (ObjectUtil.isNotEmpty(subAccountList)) {
                    for (YkfCustomerSubAccountEntity subAccount : subAccountList) {
                        YkfSubAccountManageExcel excel = new YkfSubAccountManageExcel();
                        BeanUtil.copyProperties(subAccount, excel);
                        excelList.add(excel);
                    }
                }
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("子账号信息", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            EasyExcel.write(response.getOutputStream(), YkfSubAccountManageExcel.class)
                    .sheet("子账号信息")
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(widthStyleStrategy)
                    .doWrite(excelList);
        }
    }


    @Override
    public void subAccountStop(ExcelReq req, HttpServletResponse response) throws IOException {
        //查询列表
        ParamsModel<YkfCustomerSubAccountEntity> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<YkfCustomerSubAccountEntity>() {
        }));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = stopPage(params);
        List<SubAccountModel> dataList = (List<SubAccountModel>) pageUtils.getList();
        List<YkfSubAccountStopExcel> excelList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(dataList)) {
            for (SubAccountModel item : dataList) {
                if (ObjectUtil.isNotEmpty(item.getSubAccountList())) {
                    for (YkfCustomerSubAccountEntity subAccount : item.getSubAccountList()) {
                        YkfSubAccountStopExcel excel = new YkfSubAccountStopExcel();
                        BeanUtil.copyProperties(subAccount, excel);
                        excelList.add(excel);
                    }
                }
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("已停用子账号", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            EasyExcel.write(response.getOutputStream(), YkfSubAccountStopExcel.class)
                    .sheet("已停用子账号")
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(widthStyleStrategy)
                    .doWrite(excelList);
        }
    }

    @Override
    public void customerReception(ExcelReq req, HttpServletResponse response) throws IOException {
        //查询列表
        ParamsModel<YkfQualityTestingModel> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<YkfQualityTestingModel>() {
        }));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = ykfQualityTestingService.receptionPage(params);
        List<YkfQualityTestingModel> dataList = (List<YkfQualityTestingModel>) pageUtils.getList();
        List<CustomerReceptionExcel> excelList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(dataList)) {
            if (ObjectUtil.isNotEmpty(dataList)) {
                for (YkfQualityTestingModel item : dataList) {
                    CustomerReceptionExcel excel = new CustomerReceptionExcel();
                    BeanUtil.copyProperties(item, excel);
                    excelList.add(excel);
                }
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("客服接待一览表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            EasyExcel.write(response.getOutputStream(), CustomerReceptionExcel.class)
                    .sheet("客服接待一览表")
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(widthStyleStrategy)
                    .doWrite(excelList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void saveSubAccount(SubAccountModel subAccountModel) {
        if (ObjectUtil.isNotEmpty(subAccountModel.getSubAccountList())) {
            Long shopId = subAccountModel.getShopId();
            YkfCustomerShopEntity shop = customerShopService.getById(shopId);
            Long customerId = shop.getCustomerId();
            YkfCustomerEntity customerEntity = customerService.getById(customerId);
            List<YkfCustomerSubAccountEntity> saveList = new ArrayList<>();
            for (YkfCustomerSubAccountEntity item : subAccountModel.getSubAccountList()) {
                if (StrUtil.isNotBlank(item.getAccountNo()) && StrUtil.isNotBlank(item.getAccountName()) && StrUtil.isNotBlank(item.getAccountPassword())) {
                    YkfCustomerSubAccountEntity saveItem = new YkfCustomerSubAccountEntity();
//                    saveItem.setBaseSalary(shop.getBaseSalary());
                    BeanUtil.copyProperties(shop, saveItem);
                    saveItem.setStartDate(null);
                    saveItem.setEndDate(null);
                    saveItem.setRemark("");
                    saveItem.setCreateTime(null);
                    saveItem.setCreateUserId(null);
                    saveItem.setCreateUserName(null);
                    saveItem.setCustomerName(customerEntity.getCustomerName());
                    saveItem.setAccountNo(item.getAccountNo());
                    saveItem.setAccountName(item.getAccountName());
                    saveItem.setAccountPassword(item.getAccountPassword());
                    saveItem.setStatus(1);
                    saveItem.setPlatformNo(shop.getPlatformNo());
                    saveItem.setPlatformName(shop.getPlatformName());
                    saveList.add(saveItem);
                }
            }
            if (!saveList.isEmpty()) {
                this.saveBatch(saveList);
            }
        }
    }

}
